package Event_Listener3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Chat extends JFrame{
    JTextArea txt;
    JTextField input;
    public Chat()
    {
        this.setSize(350, 200);
        this.setTitle("BorderLayout Demo.");
        // 默认this.setLayout(new BorderLayout());

        txt = new JTextArea();
        txt.setEditable(false); // 设置文本域不可编辑
        JLabel j = new JLabel("发言:");
        input = new JTextField();
        JButton btn = new JButton("发送");
        // 向btn注册事件监听器
        btn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                txt.append(input.getText()+'\n'); // 将文本框的内容输入带文本域中
                input.setText(""); // 清空文本框
            }
        });

        JPanel jp = new JPanel(); // JPanel 默认 FlowLayout
        jp.setLayout(new BorderLayout());
        jp.add(j,BorderLayout.WEST);
        jp.add(input,BorderLayout.CENTER);
        jp.add(btn,BorderLayout.EAST);
        this.add(jp,BorderLayout.SOUTH);
        this.add(txt,BorderLayout.CENTER);

        this.setLocationRelativeTo(null); // 将窗口居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 响应窗口关闭按钮
        this.setVisible(true);  // 将窗口显示出来
    }

    public static void main(String[] args)
    {
        new Event_Listener1.Chat();
    }
}